const key = 'user_info'
const maxAge = 1000 * 60 * 60 * 24 * 60

export default {
  get() {
    try {
      const user = wx.getStorageSync(key)
      if (!user || user.time + maxAge < Date.now()) return null
      return user || {}
    } catch (e) {
      return {}
    }
  },
  set(user) {
    if (!user) return null
    user.time = Date.now()
    const oldUser = this.get() || {}
    const newUser = { ...oldUser, ...user }
    console.log(newUser, 'newuser')
    wx.setStorageSync(key, newUser)
    return newUser
  },
  clear() {
    const user = this.get() || {}
    const { regionCode = '', currentCity = '', shop_id = '' } = user
    wx.clearStorageSync()
    this.set({ regionCode, currentCity, shop_id })
    return user
  }
}
